package com.cc.mall.web;

import com.cc.common.utils.R;
import com.cc.common.utils.ResponseData;
import com.cc.mall.service.ProductDetailService;
import com.cc.mall.vo.Cart;
import com.cc.user.entity.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/mall")
@Api(tags = "购物车")
public class CartController {

    @Autowired
    private ProductDetailService productDetailService;

    @ApiOperation("加入购物车")
    @GetMapping("/addToCart")
    public ResponseData addToCart(@ApiParam("商品详情id") @RequestParam("skuId") Long skuId,
                                  @ApiParam("数量") @RequestParam(value = "num",defaultValue = "1") Integer num,
                                  HttpSession session){
        UserEntity user = (UserEntity) session.getAttribute("loginuser");
        Long uid = user.getUid();
        productDetailService.addToCart(skuId,num,uid);
        return ResponseData.success("成功加入购物车",true);
    }

    @ApiOperation("获取购物车")
    @GetMapping("/cart")
    public ResponseData<Cart> cartItemList(HttpSession session){
        UserEntity user = (UserEntity) session.getAttribute("loginuser");
        Long uid = user.getUid();
        Cart cart = productDetailService.getCart(uid);
        return ResponseData.success(cart);
    }

    @ApiOperation("删除购物车中的购物项")
    @PostMapping("/deleteCartItem")
    public ResponseData deleteCartItem(@ApiParam("删除的商品详情id数组") @RequestBody Long[] skuIds,
                       HttpSession session){
        UserEntity user = (UserEntity) session.getAttribute("loginuser");
        Long uid = user.getUid();
        productDetailService.deleteCartItem(skuIds,uid);
        return ResponseData.success("删除成功",true);
    }

    @ApiOperation("对购物车中的购物项进行勾选")
    @PostMapping("/updateStatus")
    public ResponseData updateStatus(@ApiParam("购物车中的商品详情id数组") @RequestBody Long[] skuIds,
                          HttpSession session){
        UserEntity user = (UserEntity) session.getAttribute("loginuser");
        Long uid = user.getUid();
        productDetailService.updateStatus(skuIds,uid);
        return ResponseData.success("对购物车中的购物项进行勾选或取消",true);
    }

}
